// chooseAddress.js
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        checked: 'checked'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        // console.log(app)
        // console.log(app.globalData.openid)
        that.setData({
            goodsId: options.goodsId,
            priceId: options.priceId,
            count: options.count,
            content: options.content
        })
        wx.request({
            url: app.data.baseUrl + '/AibraUsb/address/list.do',
            data: {
                openId: app.globalData.openid,

            },
            success: function(res) {
                // res.data.addressList[0][4] = 1
                that.setData({
                    addressList: res.data.addressList,

                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    defaultAddress: function(e) {
        var that = this;
        var index = e.currentTarget.dataset.index;

        wx.request({
            url: app.data.baseUrl + "/AibraUsb/address/setIsDefault.do",
            data: {
                openId: app.globalData.openid,
                addressId: that.data.addressList[index][0]
            },
            success: function(res) {
                wx.redirectTo({
                    url: '../firmOrder?addressId=' + that.data.addressList[index][0] + '&goodsId=' + that.data.goodsId + '&count=' + that.data.count + '&priceId=' + that.data.priceId + '&content=' + that.data.content
                })
            }
        })
    },
    delAddress: function(e) {
        var addressId = e.currentTarget.id.slice(5);
        var that = this;
        wx.request({
            url: app.data.baseUrl + '/AibraUsb/address/delete.do',
            data: {
                openId: app.globalData.openid,
                addressId: addressId
            },
            success: function(res) {
                wx.request({
                    url: app.data.baseUrl + '/AibraUsb/address/list.do',
                    data: {
                        openId: app.globalData.openid,
                    },
                    success: function(res) {
                        that.setData({
                            addressList: res.data.addressList
                        })
                    }
                })
            }
        })
    },
    bindaddDeliveryAddress: function(e) {
        var addressId = e.currentTarget.id;
        wx.navigateTo({
            url: "../../deliveryAddress/addDeliveryAddress/addDeliveryAddress"
        })
    },
    bindAddress: function(e) {

        wx.request({
            url: app.data.baseUrl + '/AibraUsb/address/updateUI.do',
            data: {
                addressId: e.currentTarget.id
            },
            success: function(res) {
                var region = res.data.address.region;
                wx.request({
                    url: 'http://www.ttlshop.cn/AibraUsb/address/update.do',
                    data: {
                        id: res.data.address.id,
                        name: res.data.address.name,
                        sex: res.data.address.sex,
                        mobile: res.data.address.mobile,
                        address: res.data.address.address,
                        openId: app.globalData.openid,
                        region: region,
                        status: res.data.address.status
                    },
                    success: function(res) {
                        console.log('rsssssss', res)
                    }
                })
                wx.redirectTo({
                    url: "../../deliveryAddress/xiugaiAddress/xiugaiAddress?address=" + res.data.address.address + '&id=' + res.data.address.id + '&mobile=' + res.data.address.mobile + '&name=' + res.data.address.name + '&sex=' + res.data.address.sex + '&isDefault=' + res.data.address.isDefault + '&region=' + res.data.address.region
                })
            }
        })
    }
})